package com.wenqiner.requesttool.core.tool;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.RandomUtil;
import com.wenqiner.requesttool.core.constants.SystemMethodConstant;
import com.wenqiner.requesttool.function.CustomThreeParamFunction;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 占位符方法工具类
 * @author rhf
 * @date 2023-02-28
 **/
public class PlaceholderMethod {
    private static PlaceholderMethod instance = new PlaceholderMethod();

    public static final HashMap<String, Supplier<Object>> SUPPLIER_MAP = new HashMap<>();
    public static final HashMap<String, Consumer<Object>> CONSUMER_MAP = new HashMap<>();
    public static final HashMap<String, Function<Object,Object>> FUNCTION_MAP = new HashMap<>();
    public static final HashMap<String, CustomThreeParamFunction<Object,Object,Object,Object>> FUNCTION_THREE_PARAM_MAP = new HashMap<>();

    static {
        //base64转码
        FUNCTION_MAP.put(SystemMethodConstant.BASE64ENCODER, x -> Base64Encoder.encode(x.toString()));
        //base64解码
        FUNCTION_MAP.put(SystemMethodConstant.BASE64DECODER, x -> Base64Decoder.decodeStr(x.toString()));
        //获取时间戳
        SUPPLIER_MAP.put(SystemMethodConstant.TIMESTAMP, System::currentTimeMillis);
        //随机字符串
        FUNCTION_MAP.put(SystemMethodConstant.RANDOMSTRING, length -> RandomUtil.randomString(Integer.parseInt(length.toString())));
        //随机小数
        FUNCTION_THREE_PARAM_MAP.put(SystemMethodConstant.RANDOMDECIMAL, (min, max, scale) -> randomDecimal(new BigDecimal(min.toString()), new BigDecimal(max
                .toString()), Integer.parseInt(scale.toString())));
        //随机整数
        FUNCTION_MAP.put(SystemMethodConstant.RANDOMNUMBER, limit -> RandomUtil.randomInt(Integer.parseInt(limit.toString())));
    }

    public static PlaceholderMethod getInstance() {
        if (null == instance) {
            synchronized (PlaceholderMethod.class) {
                instance = new PlaceholderMethod();
            }
        }
        return instance;
    }

    private PlaceholderMethod() {
    }

    public static BigDecimal randomDecimal(BigDecimal min, BigDecimal max, Integer scale) {
        return RandomUtil.randomBigDecimal(min, max).setScale(scale, RoundingMode.UP);
    }


    /**
     * 执行方法
     *
     * @param methodName 方法名称
     * @param objects    参数
     * @return java.lang.Object
     * @author rhf
     */
    public Object execute(String methodName, Object... objects) {
        if (SUPPLIER_MAP.containsKey(methodName)) {
            return SUPPLIER_MAP.get(methodName.toUpperCase()).get();
        }
        if (CONSUMER_MAP.containsKey(methodName)) {
            CONSUMER_MAP.get(methodName.toUpperCase()).accept(objects[0]);
        }
        if (FUNCTION_MAP.containsKey(methodName)) {
            return FUNCTION_MAP.get(methodName.toUpperCase()).apply(objects[0]);
        }
        if (FUNCTION_THREE_PARAM_MAP.containsKey(methodName)) {
            return FUNCTION_THREE_PARAM_MAP.get(methodName.toUpperCase()).apply(objects[0], objects[1], objects[2]);
        }
        return null;
    }

}

